package com.dhl.fin.api.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.common.util.JsonUtil;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.SpringContextUtil;
import com.dhl.fin.api.dto.QueryPayOrderResponseDTO;
import com.dhl.fin.api.dto.RisePayOrderReqDTO;
import com.dhl.fin.api.dto.RisePayOrderResponseDTO;
import com.dhl.fin.api.dto.WeiXinRefundDTO;
import com.dhl.fin.api.service.PaymentRecordService;
import com.dhl.fin.api.service.WeixinPayService;
import com.wechat.pay.java.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * 微信支付
 */
@Slf4j
@RestController
@RequestMapping("weixin")
public class WeixinPayController {

    @Autowired
    private WeixinPayService weixinPayService;

    @Autowired
    private PaymentRecordService paymentRecordService;

    /**
     * 拉起支付订单
     */
    @PostMapping("/rise/pay/order")
    public ApiResponse risePayOrder(@RequestBody RisePayOrderReqDTO risePayOrderReqDTO) {
        log.info("risePayOrderReqDTO:{}", risePayOrderReqDTO);
        String outTradeNo = IdUtil.simpleUUID();
        RisePayOrderResponseDTO orderInfo;
        try {
            //拉起微信支付
            String prepayId = weixinPayService.risePayOrder(risePayOrderReqDTO.getTotalMoney(),
                    risePayOrderReqDTO.getDescription(),
                    outTradeNo
            );

            orderInfo = weixinPayService.createJsapiSignature(prepayId, outTradeNo);

        } catch (Exception e) {
            log.error("拉起微信支付失败", e);
            return ApiResponse.error("拉起微信支付失败");
        }

        try {
            paymentRecordService.savePaymentRecord(risePayOrderReqDTO, outTradeNo);
        } catch (Exception e) {
            log.error("保存支付数据失败", e);
            return ApiResponse.error("保存支付数据失败");
        }

        return ApiResponse.success(orderInfo);

    }


    /**
     * @param refundDTO 发起退款
     * @return
     */
    @PostMapping("/refund")
    public ApiResponse refund(@RequestBody @Validated WeiXinRefundDTO refundDTO) {
        try {
            weixinPayService.refundOrder(refundDTO);
        } catch (ServiceException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.error("退款失败");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResponse.error("后台异常");
        }
        return ApiResponse.success("退款成功");
    }

    /**
     * 查询订单状态
     * <p>
     * outTradeNo：微信支付订单号
     */
    @PostMapping("/queryOrderStatus")
    public ApiResponse payComplete(@RequestBody Map params) {
        QueryPayOrderResponseDTO orderInfo = weixinPayService.queryStatusByOrderNumber(MapUtil.getString(params, "outTradeNo"));
        return ApiResponse.success(orderInfo);
    }


    /**
     * 解密
     *
     * @param
     * @return
     */
//    @PostMapping("/decrypt")
//    public ApiResponse decrypt(@RequestBody WeXinPayCallBackDTO weXinPayCallBackDTO) {
//        try {
//            return ApiResponse.success(weixinPayService.decryptCiphertext(weXinPayCallBackDTO.getCiphertext(), weXinPayCallBackDTO.getNonce(), weXinPayCallBackDTO.getAssociatedData()));
//        } catch (Exception e) {
//            log.error("密文解密失败，{}", JsonUtil.objectToString(weXinPayCallBackDTO), e);
//            return ApiResponse.error("解密失败");
//        }
//    }


}




